<?
/************************************************************************/
/* 	The author of this program code:                                    */
/*  Dmitry Sheiko (sheiko@cmsdevelopment.com)	                    	*/
/* 	Copyright by Dmitry Sheiko											*/
/* 	http://www.cmsdevelopment.com     									*/
/************************************************************************/

/**
* @return void
* @param string $file
* @desc RSS Feed 
**/

function startElement($parser, $name, $attrs) {
	global $depth, $objdata, $structure, $counter;
	$structure[$depth[$parser]+1] = $name;
	if($name=="ITEM") $counter++;
	$depth[$parser]++;
}

function endElement($parser, $name) {
	global $depth;
	$depth[$parser]--;
}

function characterData($parser, $data) {
	global $depth, $objdata, $structure, $counter;
	if($structure[$depth[$parser]-1]=="ITEM") {
		$objdata[$counter][$structure[$depth[$parser]]] .= $data;
	}

}

function get_rss($file="http://xmlsapiens.org/rssc/"){
	global $sapi_obj, $env, $http_path, $depth, $objdata, $structure, $counter;
	
	if(preg_match("/,/", $file)) return get_rsslist($file); 

	if(!$file) {
	if($env["argv"][3]=="cmswire") $file="http://www.cmswire.com/index.rdf";
	if($env["argv"][3]=="gilbane.com") $file="http://www.gilbane.com/news.pl/7/news.xml";
	if($env["argv"][3]=="devarticles") $file="http://www.devarticles.com/index2.php?option=mos_rss&no_html=1";
	if($env["argv"][3]=="eweekdev") $file="http://rssnewsapps.ziffdavis.com/eweekdeveloper.xml";
	if($env["argv"][3]=="xmlcover") $file="http://xml.coverpages.org/covernews.xml";
	}


	$xml_parser = xml_parser_create();
	xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
	xml_set_element_handler($xml_parser, "startElement", "endElement");
	xml_set_character_data_handler($xml_parser, "characterData");
	if (!($fp = @fopen($file, "r"))) {
		return array("0"=>array("error"=>"<b style=\"color: red;\">Error:</b> <b>" . $file . " is unavailable for now... Sorry.</b>"));
	}

	while ($data = fread($fp, 4096)) {
		if (!xml_parse($xml_parser, $data, feof($fp))) {
			die(sprintf("XML error: %s at line %d",
			xml_error_string(xml_get_error_code($xml_parser)),
			xml_get_current_line_number($xml_parser)));
		}
	}
	xml_parser_free($xml_parser);
	
	if($objdata) {
		foreach($objdata as $index=>$fetch) {
		     if(isset($fetch["CONTENT:ENCODED"])) $objdata[$index]["DESCRIPTION"] = $fetch["CONTENT:ENCODED"];
		}
	}
	
	return $objdata;
}

/**
* Return list of RSS channels
*
* @param string list URLs
* @return array
**/

function get_rsslist($ListStr) {
	// $ListStr = "http://aa.com, http://bb.com";
	$ListArr = split(",", $ListStr);
	$output = array();
	 foreach($ListArr as $ChannelURL) {
	   $data = get_rss(trim($ChannelURL));
      foreach($data as $fetch) {
	         $output[$fetch["LINK"]] = $fetch;
	      }
	 }
	return $output;
}
?>